package flex.cc.store.api;

import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import flex.cc.common.utils.DateUtil;
import flex.cc.core.support.BaseController;
import flex.cc.order.manage.model.CancelNoticeModel;
import flex.cc.order.manage.service.CancelNoticeService;
import flex.cc.store.service.DispenseCouponService;

/**
 * 上游统一发券接口
 */
@RestController
@RequestMapping(value = "/storeapi")
public class CouponController extends BaseController {

	@Resource
    private DispenseCouponService dispenseCouponService;
	@Resource
    private CancelNoticeService cancelNoticeService;
    /**
     * 上游发券接口
     */
	@SuppressWarnings("unchecked")
	@ResponseBody
    @RequestMapping("/dispenseCoupon")
    public JSONObject dispenseCoupon(HttpServletRequest request) throws Exception {
		JSONObject jsonResult = new JSONObject();
        // 进入接口时间
        long startTime = System.currentTimeMillis();
        logger.info("storeapi/dispenseCoupon 进入接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));

        Boolean flag = true;
        String responseMsg = "成功";
        //读取报文
        try {
            // 设置编码
            request.setCharacterEncoding("UTF-8");
            // 读取请求报文
            String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
            requestJson = URLDecoder.decode(requestJson, "UTF-8");
            if (requestJson != null && !"".equals(requestJson)) {
                // 调用业务处理 =============================
            	logger.info("storeapi/dispenseCoupon requestJson:" + requestJson);
                JSONObject requestBody = JSONObject.parseObject(requestJson);
                Map<String, Object> requestBodyMap = requestBody.toJavaObject(Map.class);
                //请求下游
                //1、订单号如果在数据库中已存在 ，则直接返回数据库中保存的服务包和卡券数据
                //2、订单号如果在数据库中不存在 ，则请求下游服务包发送接口获取服务包和卡券，并返回上游、服务包和卡券数据保存在数据库中
                flag = true;
                jsonResult = dispenseCouponService.dispenseCoupon(requestBodyMap); 
                logger.info("storeapi/dispenseCoupon： "+jsonResult);
            } else {
                flag = false;
                responseMsg = "接口处理异常:请求报文为空";
                logger.error("storeapi/dispenseCoupon 接口处理异常:请求报文为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            responseMsg = "数据接收异常";
            logger.error("storeapi/dispenseCoupon 数据接收异常:" + e.getMessage());
        }
        if(!flag){
        	// 生成响应报文
            jsonResult.put("responseCode", "01");
        	jsonResult.put("responseMsg", responseMsg);
        }
        logger.info("storeapi/dispenseCoupon responseJson:" + jsonResult);
        // 离开接口时间
        long endTime = System.currentTimeMillis();
        logger.info("storeapi/dispenseCoupon 离开接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ";处理时长:" + (endTime - startTime));

        return jsonResult;
    }
}
